pacman::p_load(readxl, gifski, gapminder, plotly, gganimate, tidyverse)Hands-on Exercise 3_2: Programming Animated Statistical Graphics with R
1. Overview of animation
1.1 Concepts
The workflow of animations:
Dataset -> Many Data subsets by animated variable(i.e. time horizons) -> Create many individual plots by subsets -> Render motion by stitching the plots into frames and displaying them sequentially over time
1.2 Terminology
Frame: In an animated line graph, each frame represents a different point in time or a different category. When the frame changes, the data points on the graph are updated to reflect the new data
Animation Attributes: The animation attributes are the settings that control how the animation behaves.
2. Getting Started
2.1 Install R Packages
In this hand-on exercise, 5 R packages will be used, they are:
plotly: for plotting interactive statistical graphs
gganimate: an ggplot extension for creating animated statistical graphs
gifski: converts video frames to GIF animations
gapminder: An excerpt of the data available at Gapminder.org
tidyverse: designed to support data science, analysis and communication task including creating static statistical graphs
2.2 Import data
col <- c("Country", "Continent")
globalPop <- read_xls("data/GlobalPopulation.xls",
sheet = "Data") %>%
mutate_at(col, as.factor) %>%
mutate(Year = as.integer(Year))The “Data” sheet shows the population for specific year in some countries with the percentage of Young and Old.
3. Animated Data Visualization: gganimate methods
3.1 Functions inside gganimate
transition_*(): defines how the data should be spread out and how it relates to itself across time
view_*(): defines how the positional scales should change along the animation
shadow_*(): defines how data from other points in time should be presented in the given point in time
enter_*()/exit_*(): defines how data from other points in time should be presented in the given point in time
ease_aes(): defines how different aesthetics should be eased during transitions
3.2 Build a static population bubble plot
Use basic ggplot2 functions to create a static bubble plot.
ggplot(globalPop, aes(x=Old, y=Young,
size=Population,
colour=Country))+
geom_point(alpha=0.7,
show.legend=FALSE)+
scale_colour_manual(values=country_colors)+
scale_size(range = c(2,12))+
labs(title = 'Year: {frame_time}',
x='% Aged',
y='% Young')
3.3 Build the animated bubble plot
Create animated bubble plot by using:
transition time(): to create transition through distinct states in time (i.e. Year)ease_aes(): to control easing of aesthetics. The default is linear. Other methods are: quadratic, cubic, quartic, quintic, sine, circular, exponential, elastic, back, and bounce.
ggplot(globalPop, aes(x=Old,y=Young,
size=Population,
colour=Country))+
geom_point(alpha=0.7,
show.legend = FALSE)+
scale_colour_manual(values = country_colors)+
scale_size(range=c(2,12))+
labs(title="Year:{frame_time}",
x="% Aged",
y="% Young")+
transition_time(Year)+
ease_aes("linear")
4. Animated Data Visualization: plotly
frame: defines how the data should be spread out and how it relates to itself across timeids: ensure smooth transitions between objects with the same id (which helps facilitate object constancy)
4.1 Build an animated bubble plot with ggplotly()
gg <- ggplot(globalPop,
aes(x=Old,
y=Young,
size=Population,
colour=Country))+
geom_point(aes(size=Population,
frame=Year),
alpha=0.7,
show.legend=FALSE)+
scale_colour_manual(values=country_colors)+
scale_size(range=c(2,12))+
labs(x="% Aged",y="% Yound")
ggplotly(gg)gg <- ggplot(globalPop,
aes(x=Old,
y=Young,
size=Population,
colour=Country))+
geom_point(aes(size=Population,
frame=Year),
alpha=0.7,
show.legend=FALSE)+
scale_colour_manual(values=country_colors)+
scale_size(range=c(2,12))+
labs(x="% Aged",y="% Yound")+
theme(legend.position ="none")
ggplotly(gg)4.2 Build an animated bubble plot with plot_ly()
bp <- globalPop %>%
plot_ly(x=~Old,
y=~Young,
size=~Population,
color=~Continent,
sizes=c(2,100),
frame=~Year,
text=~Country,
hoverinfo="text",
type="scatter",
mode="markers") %>%
layout(showlegend=FALSE)
bp